package com.ckr.dao;

import com.ckr.pojo.Department;
import com.ckr.pojo.Employee;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Shadowckr
 * @create 2022-01-14 8:49
 */

/*
@Autowired注解，它可以对类成员变量、方法及构造函数进行标注，完成自动装配的工作。
通过 @Autowired的使用来消除set，get方法。
 */
//注册到IOC容器中
@Repository
public class EmployeeDao {
    //模拟数据库中员工表的数据
    private static Map<Integer, Employee> employees = null;

    //自动装配
    @Resource
    private DepartmentDao departmentDao;

    static {
        employees = new HashMap<>();//创建一个员工表
        employees.put(1, new Employee(1, "Cayde 6", "1234@qq.com", 1, new Department(1, "技术部"), new Date()));
        employees.put(2, new Employee(2, "Alex", "5678@qq.com", 0, new Department(2, "市场部"), new Date()));
        employees.put(3, new Employee(3, "Bill", "4321@qq.com", 1, new Department(3, "调研部"), new Date()));
        employees.put(4, new Employee(4, "Dandelion", "8765@qq.com", 1, new Department(4, "后勤部"), new Date()));
        employees.put(5, new Employee(5, "Jack", "1357@qq.com", 0, new Department(5, "运营部"), new Date()));
    }

    //主键自增（静态变量）
    private static Integer initialID = 6;

    //增加一个员工
    public void addEmployee(Employee employee) {
        if (employee.getId() == null)
            employee.setId(initialID++);
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(), employee);
    }

    //查询全部员工信息
    public Collection<Employee> getAllEmployees() {
        return employees.values();
    }

    //通过id查询员工
    public Employee getEmployeeByID(Integer id) {
        return employees.get(id);
    }

    //通过id删除员工
    public void deleteEmployeeByID(int id) {
        employees.remove(id);
    }
}
